/**
 * hibernate 分页对象.
 * 包含分页信息.
 */
package com.cr.backbone.hibenate;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Vector;

/**
 * @project RaedNote
 * @version 1.0
 * @company
 * @fileDescribe
 * @fileType Pagination Pagination.java
 * @author xianghua.hu
 * @date 2012-2-26 下午03:15:55
 */
public class Pagination {

	public final static Integer SHOW_PAGE_ROWS_2 = 2;
	public final static Integer SHOW_PAGE_ROWS_5 = 5;
	public final static Integer SHOW_PAGE_ROWS_10 = 10;
	public final static Integer SHOW_PAGE_ROWS_15 = 15;
	public final static Integer SHOW_PAGE_ROWS_20 = 20;
	public final static Integer SHOW_PAGE_ROWS_50 = 50;
	public final static Integer SHOW_PAGE_ROWS_100 = 100;
	public final static Integer SHOW_PAGE_ROWS_500 = 500;
	public final static Integer SHOW_PAGE_ROWS_1000 = 1000;

	public Integer[] getShowPageRows(){
		return new Integer[]{
				SHOW_PAGE_ROWS_2,
				SHOW_PAGE_ROWS_5,
				SHOW_PAGE_ROWS_10,
				SHOW_PAGE_ROWS_20,
				SHOW_PAGE_ROWS_50,
				SHOW_PAGE_ROWS_100,
				SHOW_PAGE_ROWS_500 ,
				SHOW_PAGE_ROWS_1000
				};
	}

	private Collection items;// 分页内容列表
	private Integer currentPage;// 当前页码
	private Integer crruntMaxPage;// 当前最大页码
	private Integer crruntMinPage;// 当前最小页码
	private Integer crruntTotalRows ;// 当前页记录总数
	private Integer queryPageTotalRows=SHOW_PAGE_ROWS_15;// 查询记录总数
	private Integer queryPage;// 查询页码
	private Integer queryFirstRows=-1;//分页查询起始行，从0开始
	
	//分页跳转
	private Vector<Integer> upperps=new Vector<Integer>();	//	上页页码集合，最大10页
	private Vector<Integer> nextps=new Vector<Integer>();	//	下页页码集合，最大10页
	private int upper=-1;									//上页 
	private int next=-1;									//下页
	
	public Pagination() {

	}

	public Pagination(Collection items) {
		super();
		this.items = items;
	}

	public Pagination(Collection items, Integer currentPage,
			Integer crruntTotalRows, Integer crruntMaxPage,
			Integer crruntMinPage, Integer queryPageTotalRows, Integer queryPage) {
		super();
		this.items = items;
		this.currentPage = currentPage;
		this.crruntTotalRows = crruntTotalRows;
		this.crruntMaxPage = crruntMaxPage;
		this.crruntMinPage = crruntMinPage;
		this.queryPageTotalRows = queryPageTotalRows;
		this.queryPage = queryPage;
	}

	public Collection getItems() {
		return items;
	}

	public void setItems(Collection items) {
		this.items = items;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getCrruntTotalRows() {
		return crruntTotalRows;
	}

	public void setCrruntTotalRows(Integer crruntTotalRows) {
		this.crruntTotalRows = crruntTotalRows;
	}

	public Integer getCrruntMaxPage() {
		return crruntMaxPage;
	}

	public void setCrruntMaxPage(Integer crruntMaxPage) {
		this.crruntMaxPage = crruntMaxPage;
	}

	public Integer getCrruntMinPage() {
		return crruntMinPage;
	}

	public void setCrruntMinPage(Integer crruntMinPage) {
		this.crruntMinPage = crruntMinPage;
	}

	public Integer getQueryPageTotalRows() {
		return queryPageTotalRows;
	}

	public void setQueryPageTotalRows(Integer queryPageTotalRows) {
		this.queryPageTotalRows = queryPageTotalRows;
	}


	/**
	 * 初始化页面信息
	 * @methodDescribe
	 * @param queryPageTotalRows2
	 * @author xianghua.hu
	 * @date 2012-2-26 下午10:07:04
	 */
	public void autoPageInfo(int crruntTotalRows) {
		// 总记录数
		this.crruntTotalRows=crruntTotalRows;//总记录数量
		// 最大页数
		this.crruntMaxPage =this.crruntTotalRows /this.queryPageTotalRows;// 最大页数=总记录数量/每页行数
		if(crruntTotalRows % this.queryPageTotalRows > 0)
			this.crruntMaxPage+=1;//存在余数，最大页数+1
		//最小页数
		this.crruntMinPage=1;
		
		//当前页数 约等于 查询页数
		this.queryPage = this.queryPage ==null?-1:this.queryPage ;//初始化查询分页为-1
		if( this.queryPage < crruntMinPage )
			this.currentPage = crruntMinPage;//query lt min ,current =min 当前页码为最小页数
		else if(this.queryPage > crruntMaxPage )
			this.currentPage = crruntMaxPage;//query gt max ,current =max 当前页码为最大页数
		else
			this.currentPage=queryPage;//min < query < max , current =query 当前页码等于查询页码

		//分页查询起始行，从0开始
		int _currentPage = currentPage - 1;//
		queryFirstRows = _currentPage * queryPageTotalRows;//起始行
		
		//当前页码，下一页、上一页初始化
		autoPageNo(currentPage,crruntMinPage,crruntMaxPage);
	}

	/**
	 * 
	 * @methodDescribe
	 * @param page
	 * @param crruntMinPage
	 * @param crruntMaxPage
	 * @author xianghua.hu
	 * @date 2012-5-10 下午03:08:46
	 */
	private void autoPageNo(int page ,int crruntMinPage ,int crruntMaxPage) {
		upper=page-1;
		next=page+1;
		int _up=page-1;
		int _ne=page+1;
		for( int i=1 ; i <= 10 ; i++){
			if(_up>0)
				upperps.add(_up);
			if(_ne<=crruntMaxPage)
				nextps.add(_ne);
			_up-=1;
			_ne+=1;
		}
		Collections.sort(upperps);
		System.out.println(upperps);
		System.out.println(nextps);
	}

	public Integer getQueryFirstRows() {
		return queryFirstRows;
	}

	public void setQueryFirstRows(Integer queryFirstRows) {
		this.queryFirstRows = queryFirstRows;
	}

	public Integer getQueryPage() {
		return queryPage;
	}

	public void setQueryPage(Integer queryPage) {
		this.queryPage = queryPage;
	}

	public Vector<Integer> getUpperps() {
		return upperps;
	}

	public void setUpperps(Vector<Integer> upperps) {
		this.upperps = upperps;
	}

	public Vector<Integer> getNextps() {
		return nextps;
	}

	public void setNextps(Vector<Integer> nextps) {
		this.nextps = nextps;
	}

	public int getUpper() {
		return upper;
	}

	public void setUpper(int upper) {
		this.upper = upper;
	}

	public int getNext() {
		return next;
	}

	public void setNext(int next) {
		this.next = next;
	}

}
